package com.groupbuying.settleproducer.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @Author gxy
 * @Description 时间转换工具类
 * @Date 2019/1/28 9:12
 **/
public class DateUtils {

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final String YMDHMS = "yyyy-MM-dd HH:mm:ss";
    /**
     * dd-MM-yyyy
     */
    public static final String DMY = "dd-MM-yyyy";
    /**
     * MM-dd-yyyy
     */
    public static final String MDY = "MM-dd-yyyy";
    /**
     * yyyy-MM-dd
     */
    public static final String YMD = "yyyy-MM-dd";
    /**
     * dd-MM-yyyy HH:mm
     */
    public static final String DMYHM = "dd-MM-yyyy HH:mm";

    /**
     * yyyyMMdd
     */
    public static final String YMDS = "yyyyMMdd";

    /**
     * @return java.lang.String
     * @Author gxy
     * @Description 时间转换成文本
     * @Date 9:18 2019/1/28
     * @Param [date, formatPattern]
     **/

    public static String format(Date date, String formatPattern) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat f = new SimpleDateFormat(formatPattern);
        return f.format(date);
    }


    /**
     * @return Date
     * @Author gxy
     * @Description 文本转换成时间
     * @Date 9:18 2019/1/28
     * @Param [text, formatPattern]
     **/
    public static Date parse(String text, String formatPattern) throws ParseException {
        if (StringUtils.isBlank(text)) {
            return null;
        }
        SimpleDateFormat f = new SimpleDateFormat(formatPattern);
        return f.parse(text);
    }

    /**
     * @Author cuich
     * @Description 时间戳转换成时间
     **/
    public static String DateParseToStamp(String text, String formatPattern) throws ParseException {
        if (StringUtils.isBlank(text)) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatPattern);
        long lt = new Long(text);
        Date date = new Date(lt);
        String res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * @Author cuich
     * @Description 时间转换成时间戳
     **/
    public static String StampparseToDate(String text, String formatPattern) throws ParseException {
        if (StringUtils.isBlank(text)) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatPattern);
        Date date = simpleDateFormat.parse(text);
        long ts = date.getTime();
        String res = String.valueOf(ts);
        return res;
    }

    /**
     * 获得当天00:00:00的date
     *
     * @author cuich
     * @date 2019年8月20日15:42:56
     */
    public static Date getTodayZeroTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        return zero;
    }

    /**
     * 获得当天23:59:59的date
     *
     * @author cuich
     * @date 2019年8月20日15:42:56
     */
    public static Date getendTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.MILLISECOND, 59);
        return cal.getTime();
    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @author WANGNING
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        if (date.after(begin) && date.before(end)) {
            return true;
        } else if (nowTime.compareTo(startTime) == 0 || nowTime.compareTo(endTime) == 0) {
            return true;
        } else {
            return false;
        }


    }

    // 字符串 转 日期
    public static Date strToDate(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
        }
        return date;
    }

    /**
     * 后期指定时间后几天日期
     *
     * @param time
     * @param dateNum
     * @return
     */
    public static String getAfterDay(String time, int dateNum) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        int day = calendar.get(Calendar.DATE);
        //                      此处修改为+则是获取后一天
        calendar.set(Calendar.DATE, day + dateNum);

        String lastDay = sdf.format(calendar.getTime());
        return lastDay;
    }

    /**
     * 将时间类型转为字符串类型格式
     *
     * @param date
     * @return
     */
    public static String getDateStr(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = sdf.format(date);
        return str;
    }

    public static String dealDateFormat(String oldDate) {
        Date date1 = null;
        DateFormat df2 = null;
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            Date date = df.parse(oldDate);
            SimpleDateFormat df1 = new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
            date1 = df1.parse(date.toString());
            df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {

            e.printStackTrace();
        }
        return df2.format(date1);
    }


    public static void main(String[] args) throws ParseException {
        System.out.println("---" + DateUtils.getDateStr(new Date()));

       /* SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM

        String nowDate=simpleDateFormat.format(new Date());
        System.out.println(nowDate);// new Date()为获取当前系统时间//Mon Sep 02 00:00:00 CST 2019*/
        // 生效时间
        String createTime="Thu Jan 09 08:49:49 CST 2020";
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", java.util.Locale.US);
        Date orderDate = sdf.parse(createTime);
        /*获取订单支付时间格式化*/

        System.out.println("==="+orderDate);
        String createTimes=DateUtils.getDateStr(orderDate);
        System.out.println("8888888888:"+createTimes);
        //Date startDate = orderDate;
        String endDate = DateUtils.getAfterDay(createTimes, 7);
        System.out.println("endDate:"+endDate);
        //开始时间
        Date startDates = strToDate(createTimes);
        // 失效时间
        Date endDates = strToDate(endDate);
       boolean flag = isEffectiveDate(new Date(), startDates, endDates);
       System.out.println(flag);
    }

}
